package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.BankSelectorWindow;
import com.lanlian.cayman.view.CitySelectorWindow;
import com.lanlian.cayman.view.VerifyMobileDialog;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.CityInfo;
import com.lanlian.commonlib.bean.ProvinceInfo;
import com.lanlian.commonlib.bean.ReqAddBank;
import com.lanlian.commonlib.bean.ReqSmsCode;
import com.lanlian.commonlib.bean.UtilEnumInfo;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.CustomDialog;
import com.lanlian.utillib.WorksSizeCheckUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/23 10:28
 *     desc   : 修改银行卡
 *     version: 1.0
 * </pre>
 */
public class AddBankActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtBankCard;
    private TextView mTvBankCity;
    private TextView mTvBankType;
    private EditText mEtBankBranch;
    private TextView mTvName;
    private TextView mTvFinish;

    private CitySelectorWindow mCitySelectorWindow;
    private BankSelectorWindow mBankSelectorWindow;
    private VerifyMobileDialog mVerifyMobileDialog;

    private ReqAddBank mReqAddBank;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_card, true);
        initView();
        setListeners();
        mReqAddBank = new ReqAddBank();
        if (mDBManager.getUser() != null) {
            mTvName.setText(mDBManager.getUser().getRealName());
        }
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("新增银行卡");
        mEtBankCard = findViewById(R.id.et_bank_card);
        mTvBankCity = findViewById(R.id.tv_bank_city);
        mTvBankType = findViewById(R.id.tv_bank_type);
        mEtBankBranch = findViewById(R.id.et_bank_branch);
        mTvName = findViewById(R.id.tv_name);
        mTvFinish = findViewById(R.id.tv_finish);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvFinish.setOnClickListener(this);
        mTvBankCity.setOnClickListener(this);
        mTvBankType.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvFinish);
        worksSizeCheckUtil.addAllEditText(mEtBankBranch, mEtBankCard, mTvBankCity, mTvBankType);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    mTvFinish.setBackgroundResource(R.mipmap.login_btn);
                } else {
                    mTvFinish.setBackgroundResource(R.drawable.bg_click_false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_bank_city:
                if (mCitySelectorWindow == null) {
                    mCitySelectorWindow = new CitySelectorWindow(this);
                    mCitySelectorWindow.setOnCityChooseListener(new CitySelectorWindow.OnCityChooseListener() {
                        @Override
                        public void onCityChoose(final ProvinceInfo provinceInfo, final CityInfo cityInfo) {
                            mTvBankCity.setText(String.format("%s %s", provinceInfo.getName(), cityInfo.getName()));
                            mReqAddBank.setBankCity(cityInfo.getId());
                            mReqAddBank.setBankProvince(provinceInfo.getId());
                        }
                    });
                }
                mCitySelectorWindow.showDialog();
                break;
            case R.id.tv_bank_type:
                if (mBankSelectorWindow == null) {
                    mBankSelectorWindow = new BankSelectorWindow(this);
                    mBankSelectorWindow.setOnBankChooseListener(new BankSelectorWindow.OnBankChooseListener() {
                        @Override
                        public void onBankChoose(UtilEnumInfo bank) {
                            mTvBankType.setText(bank.getName());
                            mReqAddBank.setBankType(bank.getId());
                        }
                    });
                }
                mBankSelectorWindow.showDialog();
                break;
            case R.id.tv_finish:
                mTvFinish.setClickable(false);
                VolleyRequestUtils.getInstance().sendSmsCode(this.toString(),
                        new ReqSmsCode(mDBManager.getUser().getMobileNo()), new INetWorkCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                mTvFinish.setClickable(true);
                                showToast("验证码已发送至您的手机");
                                mVerifyMobileDialog = new VerifyMobileDialog(AddBankActivity.this, mDBManager.getUser().getMobile(),
                                        new VerifyMobileDialog.OnCompleteListener() {
                                            @Override
                                            public void onComplete(VerifyMobileDialog verifyMobileDialog, String content) {
                                                addBank(content);
                                            }
                                        });
                                mVerifyMobileDialog.show();
                            }

                            @Override
                            public void onFail(BaseResponse baseResponse) {
                                mTvFinish.setClickable(true);
                                showToast(baseResponse.getMsg());
                            }
                        });
                break;
            default:
                break;
        }
    }

    /**
     * 新增银行卡
     */
    private void addBank(String code) {
        mReqAddBank.setBankCard(mEtBankCard.getText().toString());
        mReqAddBank.setBankBranch(mEtBankBranch.getText().toString());
        mReqAddBank.setCode(code);
        showLoadingDialog("提交中...");
        VolleyRequestUtils.getInstance().addBank(this.toString(), mReqAddBank, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                showToast("提交成功");
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                if (!baseResponse.getMsg().contains("验证码")) {
                    mVerifyMobileDialog.dismiss();
                } else {
                    mVerifyMobileDialog.clear();
                }
                dismissLoadingDialog();
                new CustomDialog(AddBankActivity.this, new CustomDialog.OnClickListener() {
                    @Override
                    public void onLeftClick() {
                    }

                    @Override
                    public void onRightClick() {
                    }
                }).setOneBtn(true).setRight("关闭").setTitle(baseResponse.getMsg()).show();
            }
        });
    }

}
